package wetalk.software.bupt.com.wetalk.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import wetalk.software.bupt.com.wetalk.DB.WeTalkDB;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.model.po.WeTalkRecent;


public class MessageRecentAdapter extends ArrayAdapter<WeTalkRecent>implements Filterable{
    private LayoutInflater inflater;
    private List<WeTalkRecent> mData;
    private Context mContext;
	
	public MessageRecentAdapter(Context context,int textViewResourceId,List<WeTalkRecent> objects) {
        super(context,textViewResourceId,objects);
        inflater = LayoutInflater.from(context);
        this.mContext = context;
        mData = objects;
	}

    @Override
    public int getCount() {
        return mData.size();
    }

//    @Override
//    public Object getItem(int position) {
//        return mData.get(position);
//    }
//
//    @Override
//    public long getItemId(int position) {
//        return position;
//    }

    @Override
	public View getView(int position, View convertView, ViewGroup parent) {
        final WeTalkRecent item = mData.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_conversation, parent, false);
        }
            viewHolder = new ViewHolder();
            viewHolder.iv_recent_avatar = (ImageView)convertView.findViewById(R.id.iv_recent_avatar);
            viewHolder.tv_recent_name = (TextView) convertView.findViewById(R.id.tv_recent_name);
            viewHolder.tv_recent_msg = (TextView) convertView.findViewById(R.id.tv_recent_msg);
            viewHolder.tv_recent_time = (TextView) convertView.findViewById(R.id.tv_recent_time);
            viewHolder.tv_recent_unread = (TextView) convertView.findViewById(R.id.tv_recent_unread);
            convertView.setTag(viewHolder);
            viewHolder= (ViewHolder) convertView.getTag();

//填充数据
        viewHolder.iv_recent_avatar.setImageResource(R.mipmap.a1);
        viewHolder.tv_recent_name.setText(item.getUserName());
        viewHolder.tv_recent_msg.setText(item.getMessage());
        viewHolder.tv_recent_time.setText( new SimpleDateFormat("HH:mm").format(item.getTime()));
//        viewHolder.tv_recent_unread.setText("[未读]");

        Log.i("", "数据填充成功");
        int num = WeTalkDB.create(mContext).getUnreadCount(item.getTargetid());
        if(num>0 ){
            viewHolder.tv_recent_unread.setVisibility(View.VISIBLE);
            viewHolder.tv_recent_unread.setText(num + "");
        }
        else{
            viewHolder.tv_recent_unread.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder{
        ImageView iv_recent_avatar;
        TextView tv_recent_name,tv_recent_msg,tv_recent_time,tv_recent_unread;
    }
}
